//
//  UILabel+QSLabel.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/17.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "UILabel+QSLabel.h"



@implementation UILabel (QSLabel)
+ (UILabel *)labelWithName:(NSString *)labelName font:(UIFont *)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)textAlignment{
    UILabel * label = [[UILabel alloc] init];
    if (labelName) {
        label.text = labelName;
    }
    label.textAlignment = textAlignment;
    label.font = font;
    label.textColor = color;
    return label;
}

- (void)changeLineSpaceWithSpace:(CGFloat)space textAlignment:(NSTextAlignment)textAlignment {
    if (!self.text.length) {
        return;
    }
    NSString *labelText = self.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    if (textAlignment) {
        [paragraphStyle setAlignment:textAlignment];
    }
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    [self sizeToFit];
}

//部分变色
+ (NSMutableAttributedString *)setLabelTextColorWithString:(NSString *)string
                              Range:(NSRange )range
                          andColor:(UIColor *)color{
    // 创建Attributed
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
    // 需要改变的区间(第一个参数，从第几位起，长度)
    NSRange range1 = range;
    // 改变文字颜色
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range1];
    return noteStr;
}
@end
